package me.ingxin.android.keyboard.view

import android.content.Context
import android.util.AttributeSet
import me.ingxin.android.keyboard.entry.Key

/**
 * Created by ingxin on 2023/2/6
 * 字母键盘符号部分
 */
open class SymbolKeyboardView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : BaseKeyboardView(context, attrs) {

    /**符号列表*/
    private val symbol = charArrayOf(
        '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
        '\'', '"', '=', '_', ':', ';', '?', '~', '|', '·',
        '+', '-', '\\', '/', '[', ']', '{', '}', ',', '.',
        '<', '>'
    )

    override fun onCreateKeyboard(suggestKeyWidth: Float, suggestKeyHeight: Float): List<Key> {
        val keys = ArrayList<Key>()

        //row1&row2
        var rowX = paddingStart.toFloat()
        var rowY = paddingTop.toFloat()
        for (i in symbol.indices) {
            if (i % 10 == 0) {
                //换行
                rowX = paddingStart.toFloat()
                rowY = paddingTop + (suggestKeyHeight + verticalGap) * (i / 10)
            }
            val letter = symbol[i]
            keys.add(
                Key(
                    rowX, rowY, rowX + suggestKeyWidth, rowY + suggestKeyHeight,
                    letter.code, letter.toString()
                )
            )
            rowX += suggestKeyWidth + horizontalGap
        }

        val deleteWidth = suggestKeyWidth * 1.3f
        val deleteStartX = width - paddingEnd - deleteWidth

        //删除键
        keys.add(
            Key(
                deleteStartX, rowY, deleteStartX + deleteWidth,
                rowY + suggestKeyHeight, Key.CODE_DELETE, "delete"
            )
        )

        //空格
        keys.add(
            Key(
                paddingStart + (suggestKeyWidth + horizontalGap) * 2, rowY,
                deleteStartX - horizontalGap, rowY + suggestKeyHeight, ' '.code, "space"
            )
        )

        return keys
    }

    override fun getMaxRowSize(): Int = 4

    override fun getMaxColumnSize(): Int = 10

}